package com.tianji.promotion.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.promotion.domain.dto.CouponFormDTO;
import com.tianji.promotion.domain.dto.CouponIssueFormDTO;
import com.tianji.promotion.domain.query.CouponQuery;
import com.tianji.promotion.domain.vo.CouponDetailVO;
import com.tianji.promotion.domain.vo.CouponPageVO;
import com.tianji.promotion.domain.vo.CouponVO;
import com.tianji.promotion.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 优惠券的规则信息 控制器
 * </p>
 *
 * @author xjs
 */
@Validated
@Api(tags = "Coupon管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/coupons")
public class CouponController {

    private final ICouponService couponService;


    @PostMapping
    public void addCoupon(@Valid @RequestBody CouponFormDTO dto){
        couponService.addCoupon(dto);
    }

    @GetMapping("/page")
    public PageDTO<CouponPageVO> pageQuery(CouponQuery query){
        return couponService.pageQuery(query);
    }

    @PutMapping("/{id}/issue")
    public void issue(@PathVariable("id") Long id, @Valid @RequestBody CouponIssueFormDTO dto){
        couponService.issue(id, dto);
    }


    @ApiOperation("修改优惠券")
    @PutMapping("{id}")
    public void updateCoupon(@Validated @NotNull(message = "id不能为空")@PathVariable("id")Long id, @Validated @RequestBody CouponFormDTO dto){
        couponService.updateCoupon(id, dto);
    }


    @ApiOperation("删除优惠券")
    @DeleteMapping("{id}")
    public void deleteCoupon(@Validated @NotNull(message = "id不能为空")@PathVariable("id")Long id){
        couponService.deleteCoupon(id);
    }

    @ApiOperation("根据id查询优惠券")
    @GetMapping("{id}")
    public CouponDetailVO getCoupon(@Validated @NotNull(message = "id不能为空")@PathVariable("id")Long id){
        return couponService.getCoupon(id);
    }

    @ApiOperation("暂停发放优惠券")
    @PutMapping("{id}/pause")
    public void pause(@Validated @NotNull(message = "id不能为空")@PathVariable("id")Long id){
        couponService.pause(id);
    }

    /**
     * 查询系统中的优惠券
     * 1）发放中
     * 2）手动领取
     * */
    @GetMapping("/list")
    public List<CouponVO> list(){
        return couponService.couponList();
    }

}
